package uit.uthuoc.fragment;

import java.util.ArrayList;

import uit.uthuoc.dal.DrugDAO;
import uit.uthuoc.dal.DrugDTO;
import uit.uthuoc.dal.MIMSClassDTO;
import uit.uthuoc.utils.Config;
import android.support.v7.widget.SearchView.OnQueryTextListener;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MIMSClassSearchFragment extends BaseSearchableFragment {

		
	private ArrayAdapter<MIMSClassDTO> adapter;
	private ArrayList<MIMSClassDTO> mMIMSClassList;
	private OnQueryTextListener queryListener = new OnQueryTextListener() {
	
		@Override
		public boolean onQueryTextChange(String arg0) {
			if (arg0.length() >= Config.MIN_LENGHT_TO_SEARCH) {
				onSearchButtonClicked(arg0);
			} else {
				adapter.clear();
			}
			return true;
		}

		@Override
		public boolean onQueryTextSubmit(String arg0) {
			mSearchView.clearFocus();
			return false;
		}
	};
	
	@Override
	protected void setQueryListener() {
		mSearchView.setOnQueryTextListener(queryListener);
		mSearchView.setQueryHint("Tìm kiếm theo phân loại");

	}

	@Override
	protected void iniListAdapter() {
		adapter = new ArrayAdapter<MIMSClassDTO>(getActivity(), android.R.layout.simple_list_item_1);
		adapter.clear();
		setListAdapter(adapter);

	}
	
	private void onSearchButtonClicked(String arg0) {
		adapter.clear();
		
		mMIMSClassList = DrugDAO.getDAO().searchMIMSClassByMIMSClassName(arg0);
		
		for (MIMSClassDTO item : mMIMSClassList) {
			adapter.add(item);
		}			
		setListAdapter(adapter);
		
	}
	
	public MIMSClassDTO getMIMSByPosition(int position)  {
		return mMIMSClassList.get(position);
	}
	
	

}
